package com.tf_cut.backend.platform.service.impl;


import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.mapper.GrantMapper;
import com.tf_cut.backend.platform.pojo.Grant;
import com.tf_cut.backend.platform.repository.GrantRepository;
import com.tf_cut.backend.platform.service.GrantService;
import com.tf_cut.backend.platform.vo.GrantVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
@Transactional
@Slf4j
public class GrantServiceImpl extends CommonServiceImpl<GrantVo, Grant, String> implements GrantService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private GrantRepository grantRepository;
    @Autowired
    private GrantMapper grantMapper;

    @Override
    public PageInfo<GrantVo> queryGrantList(GrantVo grantVo) {
        com.github.pagehelper.PageHelper.startPage(grantVo.getPage(), grantVo.getRows());
        List<GrantVo> list = grantMapper.queryGrantList(grantVo);
        com.github.pagehelper.PageInfo<GrantVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);

        PageInfo<GrantVo> returnPageInfo = new PageInfo<>();
        returnPageInfo.setPage(pageInfo.getPageNum());
        returnPageInfo.setPageSize(pageInfo.getPageSize());
        returnPageInfo.setRows(pageInfo.getList());
        returnPageInfo.setRecords((int) pageInfo.getTotal());
        returnPageInfo.setTotal(pageInfo.getPages());
        return returnPageInfo;
    }

    @Override
    public void updateGrantVo(GrantVo grantVo) {
        grantMapper.updateGrant(grantVo);
    }
}
